/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons;

import java.util.Arrays;
import java.util.List;

import org.dctmcommons.impl.TypeHelper;
import org.junit.*;

import com.documentum.fc.common.DfException;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

/**
 * Integration tests for {@link TypeHelper}.
 */
public class TypeHelperIT {

	/** Serial id. */
	private static final long serialVersionUID = 6633647479696146895L;

	/** The type helper instance. */
	private ITypeHelper typeHelper = null;
	
	/**
	 * Tests the factory method.
	 */
	@Test
	public void testGetInstance() {
		System.out.println(">>> testGetInstance");
		assertThat("Factory method returned null",
				   typeHelper,
				   is(notNullValue()));
	}

	/**
	 * Tests the isDescendantType method.
	 * @throws DfException The repository exception
	 */
	@Test
	public void testIsDescendantType() throws DfException {
		System.out.println(">>> testIsDescendantType");
		
		assertThat(
				"Type dm_document should be descendant of dm_sysobject",
				typeHelper.isDescendantType(DctmCommonsTestUtil.getSession(),
											"dm_sysobject",
											"dm_document"),
				is(true));
		
		assertThat(
				"Type dm_xml_config should be descendant of dm_document",
				typeHelper.isDescendantType(DctmCommonsTestUtil.getSession(),
											"dm_document",
											"dm_xml_config"),
				is(true));
		
		assertThat(
				"Type dm_xml_config should be descendant of dm_sysobject",
				typeHelper.isDescendantType(DctmCommonsTestUtil.getSession(),
											"dm_sysobject",
											"dm_xml_config"),
				is(true));
		
		DctmCommonsTestUtil.releaseSession();
	}
	
	/**
	 * Tests the getDescendantsTypes method.
	 * @throws DfException The repository exception
	 */
	@Test
	public void testGetDescendantTypes() throws DfException {
		System.out.println(">>> testGetDescendantTypes");

		final String[] docTypes = typeHelper.getDescendantTypes(
											DctmCommonsTestUtil.getSession(),
											"dm_document",
											false);
		assertThat("Descendant doc types is null",
				   docTypes,
				   is(notNullValue()));
		assertThat("Incorrect number of descendant doc types",
				   docTypes.length,
				   is(greaterThan(0)));
		
		final List<String> list = Arrays.asList(docTypes);
		assertThat("Known descendant doc type is missing: dmc_jar",
				   list,
				   hasItem("dmc_jar"));
		assertThat("Known descendant doc type is missing: dm_menu_system",
				   list,
				   hasItem("dm_menu_system"));
		assertThat("Known descendant doc type is missing: dm_xml_config",
				   list,
				   hasItem("dm_xml_config"));
		
		DctmCommonsTestUtil.releaseSession();
	}

	/**
	 * Tests the getAncestorTypes method.
	 * @throws DfException The repository exception
	 */
	@Test
	public void testGetAncestorTypes() throws DfException {
		System.out.println(">>> testGetAncestorTypes");

		final String[] docTypes = typeHelper.getAncestorTypes(
											DctmCommonsTestUtil.getSession(),
											"dm_xml_application",
											false);
		assertThat("Ancestor doc types is null",
				   docTypes,
				   is(notNullValue()));
		assertThat("Incorrect number of ancestor doc types",
				   docTypes.length,
				   is(2));
		
		final List<String> list = Arrays.asList(docTypes);
		assertThat("Known ancestor doc type is missing: dm_document",
				   list,
				   hasItem("dm_folder"));
		assertThat("Known descendant doc type is missing: dm_sysobject",
				   list,
				   hasItem("dm_sysobject"));
		
		DctmCommonsTestUtil.releaseSession();
	}
	
	/**
	 * Setup routine.
	 */
	@Before
	public void before() throws DfException {
		typeHelper = TypeHelper.getInstance();
	}
	
	/**
	 * Teardown routine.
	 */
	@After
	public void after() throws DfException {
	}
}
